<?php
class Help extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
	}
	
	function index() {
		$this->load->model ( 'Mhelp' );
		$this->load->model ( 'Muser' );
		$this->load->library ( 'pagination' );
		
		$this->load->model ( 'Mabout' );
		$data ['uid'] = $this->session->userdata ( 'uid' );
		$data ['name'] = $this->session->userdata ( 'uname' );
		$data ['cardAboutList'] = $this->Mabout->getAboutListbyClass ( 'C' );
		$data ['aboutList'] = $this->Mabout->getAboutListbyClass ( 'A' );
		
		$data ['num'] = $this->Mhelp->getQuestionNum ();
		$data ['page'] = $this->uri->segment ( 3, 0 );
		
		$config ['base_url'] = base_url () . 'help/index/';
		$config ['total_rows'] = $data ['num'];
		$config ['per_page'] = PAGE_SHOW_NUM;
		$config ['uri_segment'] = 3;
		$config ['num_links'] = 2;
		$this->pagination->initialize ( $config );
		
		$data ['questionlist'] = $this->Mhelp->getQuestionList ( $data ['page'], PAGE_SHOW_NUM );
		
		$this->load->view ( 'help_index', $data );
	}
	
	function message() {
		$this->load->helper ( 'form' );
		$this->load->model ( 'Muser' );
		
		$this->load->model ( 'Mabout' );
		$data ['uid'] = $this->session->userdata ( 'uid' );
		$data ['name'] = $this->session->userdata ( 'uname' );
		$data ['cardAboutList'] = $this->Mabout->getAboutListbyClass ( 'C' );
		$data ['aboutList'] = $this->Mabout->getAboutListbyClass ( 'A' );
		
		$this->form_validation->set_rules ( 'name', '姓名', 'trim|required|min_length[2]|max_length[10]' );
		$this->form_validation->set_rules ( 'mobile', '联系电话', 'trim|required|min_length[4]|max_length[20]|callback_checkPhoneCB' );
		$this->form_validation->set_rules ( 'details', '留言', 'trim|max_length[140]' );
		$this->form_validation->set_rules ( 'test', '验证码', 'trim|required|callback_checkCaptchaCB' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['img'] = $this->getCaptcha ();
			$data ['form'] = array ('id' => 'messageForm' );
			$this->load->view ( 'message_user', $data );
		} else {
			$this->load->model ( 'Mmessage' );
			$info ['name'] = $this->input->post ( 'name' );
			$info ['mobile'] = $this->input->post ( 'mobile' );
			$info ['details'] = $this->input->post ( 'details' );
			if ($this->Mmessage->addMessage ( $info )) {
				$data ['msg'] = 'help_message_T';
				$this->load->view ( 'message', $data );
			} else {
				$data ['msg'] = 'help_message_F';
				$this->load->view ( 'message', $data );
			}
		}
	}
	
	//功能函数
	function getCaptcha() {
		$this->load->helper ( 'captcha' );
		$this->load->helper ( 'string' );
		$vals = array ('word' => random_string ( 'numeric', 5 ), 'img_path' => './captchas/', 'img_url' => base_url () . 'captchas/', 'img_width' => 60, 'img_height' => 30, 'expiration' => 120 );
		$cap = create_captcha ( $vals );
		$this->session->set_userdata ( 'verifyCode', $cap ['word'] );
		return $cap ['image'];
	}
	
	function checkCaptchaCB($str) {
		if (strcasecmp ( $str, $this->session->userdata ( 'verifyCode' ) ) == 0) {
			return TRUE;
		} else {
			$this->form_validation->set_message ( 'checkCaptchaCB', '%s不正确' );
			return FALSE;
		}
	}
	
	function checkPhoneCB($str) {
		if (preg_match ( "/^[0-9-]{8,20}$/", $str ))
			return TRUE;
		else {
			$this->form_validation->set_message ( 'checkPhoneCB', '%s不正确' );
			return FALSE;
		}
	}
}